package com.jialanmo.zhihudaily.ui.main;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.jialanmo.helper.recyclerview.CheckableAdapter;
import com.jialanmo.helper.recyclerview.HeaderAndFooterAdapter;
import com.jialanmo.helper.recyclerview.ItemDecorationDivider;
import com.jialanmo.helper.OnItemClickListener;
import com.jialanmo.helper.recyclerview.OnViewCreateListener;
import com.jialanmo.zhihudaily.R;
import com.jialanmo.zhihudaily.data.model.Theme;
import com.jialanmo.zhihudaily.data.model.Themes;
import com.jialanmo.zhihudaily.ui.theme.ThemeFragment;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;

public final class MainActivity extends AppCompatActivity implements MainContract.ActivityView {

    private long mPreviousBackKeyDownTime;

    private final MainContract.ActivityPresenter mPresenter = new MainActivityPresenter(this);
    private final CheckableAdapter<Themes.Theme> mDrawerAdapter = new CheckableAdapter<>(new HeaderAndFooterAdapter<>(new DrawerAdapter(new OnItemClickListener() {
        @Override
        public void onItemClick(int position) {
            mDrawerAdapter.setItemChecked(position, true);
            Fragment fragment = new ThemeFragment();
            Bundle bundle = new Bundle();
            bundle.putInt(Theme.THEME_ID, mDrawerAdapter.getData().get(position).getId());
            fragment.setArguments(bundle);
            getSupportFragmentManager().beginTransaction().replace(R.id.activity_main_framelayout_fragment, fragment).commit();
            mDrawerlayout.closeDrawers();
        }
    })));

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.activity_main_drawerlayout)
    DrawerLayout mDrawerlayout;
    @BindView(R.id.activity_main_drawerlayout_recyclerview_themes)
    RecyclerView mDrawerRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        setSupportActionBar(mToolbar);

        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerlayout, mToolbar, R.string.drawer_open, R.string.drawer_close);
        mDrawerlayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();

        mDrawerAdapter.setHeaderView(new OnViewCreateListener() {
            @Override
            public View onViewCreate(ViewGroup parent) {
                TextView textView = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main_drawerlayout_theme_recyclerview_item, parent, false);
                textView.setText("首页");
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDrawerAdapter.setHeaderViewChecked(true);
                        getSupportFragmentManager().beginTransaction().replace(R.id.activity_main_framelayout_fragment, new MainFragment()).commit();
                        mDrawerlayout.closeDrawers();
                    }
                });
                return textView;
            }
        });
        mDrawerAdapter.setChoiceMode(CheckableAdapter.CHOICE_MODE_SINGLE);
        mDrawerAdapter.setHeaderViewChecked(true);

        mDrawerRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mDrawerRecyclerView.hasFixedSize();
        mDrawerRecyclerView.addItemDecoration(new ItemDecorationDivider());
        mDrawerRecyclerView.setAdapter(mDrawerAdapter);

        getSupportFragmentManager().beginTransaction().replace(R.id.activity_main_framelayout_fragment, new MainFragment()).commit();

        mPresenter.start();
    }

    @Override
    public void onBackPressed() {
        long currentTime = new Date().getTime();
        if (currentTime - mPreviousBackKeyDownTime < 2000) {
            finish();
        } else {
            Toast.makeText(this, "再按一次返回键退出程序", Toast.LENGTH_SHORT).show();
        }
        mPreviousBackKeyDownTime = currentTime;
    }

    @Override
    public void showThemes(Themes themes) {
        int insertIndex = mDrawerAdapter.getItemCount();
        mDrawerAdapter.getData().addAll(themes.getOthers());
        mDrawerAdapter.notifyItemRangeInserted(insertIndex, themes.getOthers().size());
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showMessage(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
